cc.Class({
    extends: cc.Component,

    properties: {
		alertcell:cc.Prefab,
		testsprite:cc.Sprite,
		testLabel:cc.Label,
		//powers:cc.Node,
		powerCount:0,
		batteryState:0,
		v:0,
    },

    onLoad () {
		cc.showImg = (path, w, h) => {
            // 用于显示信息
            this.testLabel.string = ('宽:',w,'高:',h,'，路径:',path);
            // 给数据传递留点时间，100ms即可
            setTimeout (() => {
                let width = parseInt(w);
                let height = parseInt(h);
                let data = jsb.fileUtils.getDataFromFile(path);
                let buffer = null;
                // 这里简单处理了，应该判断末尾
                if (path.includes("png")) {
                    let img = UPNG.decode(data);
                    buffer = UPNG.toRGBA8(img)[0];
					this.testLabel.string = 'showImg=,select UPNG---';
                }else if(path.includes("jpg")){//JPEG
                    buffer = decode(data).data;
					this.testLabel.string = 'showImg=,select UJPG';
                }else{
					this.onalert('path no png or jpg');
				}
                let array = new Uint8Array(buffer);
                let tex = new cc.Texture2D();
                tex.initWithData(array, cc.Texture2D.PixelFormat.RGBA8888, width, height);
                this.testsprite.spriteFrame = new cc.SpriteFrame(tex);
            }, 500);
			//this.testLabel.string = 'showImg=,select setTimeout---';
		}
	},
//------------------以下为ver2的代码：简单交互：显示字符串 弹窗文字 原生alert提示框
//复位testLabel
	resettestlabel(){
		this.testLabel.string = '复位testLabel';
	},
	//简单交互，显示字符串
	showLabel () {
        let className = "org/cocos2dx/javascript/AppActivity";
        let text = jsb.reflection.callStaticMethod(className, "getTestText", "()Ljava/lang/String;");
        this.testLabel.string = text + "";
    },
	//显示toast
    show () {
        this.showToast("弹窗啦！");
    },
    showToast (string) {
        let className = "org/cocos2dx/javascript/AppActivity";
        jsb.reflection.callStaticMethod(className, "showToast", "(Ljava/lang/String;)V", string);
    },

	//显示原生的alert提示框-成功
	showalert() {
		let className = "org/cocos2dx/javascript/AppActivity";
        jsb.reflection.callStaticMethod(className, "showAlertDialog", "(Ljava/lang/String;Ljava/lang/String;)V", "标题", "消息内容");
    },
	//------------------以上为ver2的代码，以下为ver3的代码
	//获取电池信息
	getdc() {
		this.getElect();
		this.updateBatteryShowState();
    },
	onalert(txt){
		let alertcell = cc.instantiate(this.alertcell);
		alertcell.parent = this.node;
		cc.director.emit('alerttext',txt);
	},
    // 根据数据调整电量格子显示
	updateBatteryShowState () {
		let n = Math.floor(this.powerCount / 10);
		this.updateBatteryShowStateByCount(n);
	},
	
	//10个格子，显示几个
	updateBatteryShowStateByCount (showCount) {
		let array = this.powers.children;
		array.forEach((ele, index) => {
			if (index < showCount) {
				ele.opacity = 255;
			} else {
				ele.opacity = 0;
			}
		})
	},

	// 根据数据显示于 Label 组件
	updateInfoShow () {
		let text =  this.powerCount +'\n'+this.batteryState+'\n'+this.v + "毫伏(mV)";
		this.testLabel.string = text;
		let txt = 'updateInfoShow:pc='+this.powerCount+',s='+this.batteryState+',v='+this.v;
		this.onalert(txt);
		this.showToast(txt);
	},
	// 约定格式 xx,xx,xx,xx 然后用 split(",") 进行切割
	getElect () {
		let className = "org/cocos2dx/javascript/AppActivity"
		let info = jsb.reflection.callStaticMethod(className, "getElect", "()Ljava/lang/String;")
		if (!info) {
			cc.log("jsb get null");
			let txt = "getElect get null！";
			this.showToast(txt);
			this.onalert(txt);
			return;
		}
		let array = info.split(",");
		let level = parseInt(array[0]);
		let scale = parseInt(array[1]);
		let status = array[2];
		let voltage = parseInt(array[3]);
		this.powerCount = (level * 100) / scale;
		this.batteryState = status;
		this.v = voltage;
		// 数据赋值完毕后刷新电池格数与 Label 显示
		this.updateInfoShow();
	}, 
	//------------------以上为ver3的代码，以下为ver4的代码
	//获取图片数据--bug:能打开相册选择图片，但是不能应用到testsprite上显示的
	//bug:点击获取img后就不能再点击其他修改testlabel的功能了，无法修改
	getImgData () {
        let className = "org/cocos2dx/javascript/AppActivity";
        jsb.reflection.callStaticMethod(className, "getImgData", "()V");
    },
    // update (dt) {},
});
